home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: beginner question
- Date: Wed, 03 Apr 96 14:22:41 GMT
- Organization: none
- Message-ID: <828541361snz@genesis.demon.co.uk>
- References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com> <315F525A.4A1CD496@alcyone.com> <31625C30.12AA@sooner.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <31625C30.12AA@sooner.net> edwbush@sooner.net "Eddie Bush" writes:
-
- >Erik Max Francis wrote:
- >>
- >> Tarang Deshpande wrote:
- >>
- >> > So then what does the following mean:
- >> >
- >> > struct _FOO
- >> > {
- >> > int bar;
- >> > } FOO;
- >> >
- >> > struct _FOO s1;
- >> > FOO s2;
- >> >
- >> > Why?
- >>
- >> This is a compiler error.
- >
- >I'm sorry. I don't see an error -- just two different variables of the same
- > type that happen to
- >be declared using two different (and correct) methods.
-
- FOO here is defined as a variable, not a type since there was no typedef.
- It is just as illegal as writing:
-
- int BAR;
- BAR y;
-
- you must write it as:
-
- typedef int BAR;
- BAR y;
-
- >> struct _FOO { int bar; } FOO;
- >>
- >> declares a structure called struct _FOO and an instance of that structure
- > FOO.
- >> The further declaration
- >
- >The initial declaration of the structure doesn't make available FOO for
- > manipulation. FOO is a
- >type. ...isn't it?
-
- As written FOO is defined as a variable, not a type.
-
- > I always understood that 'struct _FOO' is the 'tag name'
- > (the name of the
- >structure', and that FOO would be a type which you have defined (by the typedef
- > struct _FOO) to
- >be the structure _FOO.
-
- If there was a typedef there, that would be true.
-
- ...
-
- >Also, from what I understand, you would do it like this:
- >
- >typedef struct {
- > int num;
- >} FOOBAR;
- >
- >This would be if you intended to use the type statically. Or, you could:
- >
- >typedef struct FOO {
- > int num;
- >} *BAR;
- >
- >if you happened to want to allocate the variable dynamically.
-
- This defines BAR type as a pointer to a struct FOO. You can do with it what
- you can do with other pointers, e.g. make a variable of that type point
- to an object (or one of an array of objects) created by malloc.
-
- >Notice, that I left the 'tag name' off of the variable which I intend to use
- > statically. That is
- >because it is not needed -- you only need it to allocate memory dynamically:
-
- Be clear that it isn't the variable of type BAR that is in any sense 'dynamic',
- it is simply a pointer. However it can point to objects allocated dynamically.
-
- >BAR new_bar;
- >
- >new_bar = (BAR) malloc (sizeof (struct FOO));
-
- Hiding pointers behind typedefs generally confuses unless the 'pointerness'
- of the type is not important (which it is here). However sticking with
- BAR here this is probably best written as:
-
- new_bar = malloc (sizeof *new_bar);
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-